<?php
namespace app\restaurant\controller;
use app\AdminRest;

use app\BaseController;
use app\restaurant\model\Progress;
use app\restaurant\model\TransporterOrder;
use app\restaurant\server\Context;
use app\restaurant\server\DadaApi;
use think\App;

use app\restaurant\model\Order as model;

use think\facade\Db;


class SendResult extends BaseController
{

    protected $model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

    }


    public function test(){

        dump($_SERVER);exit;

        echo json_encode(1);exit;


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:达达订单回调
     */
    public function dadaSend(){
        //回调内容
        $text = json_decode( $this->request->getInput(), true );

        if(!empty($text)){

            $context = new Context($text['order_id']);

            $transporter_model = new TransporterOrder();
            //达达平台的订单信息
            $order_info = $context->orderInfo($text['order_id']);
            //如果状态不对就不执行了
            if(!empty($order_info['code'])){

                return false;
            }
            //平台的物流信息
            $info = $transporter_model->orderInfo(['order_code'=>$text['order_id']]);
            //当前的状态
//            $statusMsg  = !empty($info['status_msg'])?unserialize($info['status_msg']):[];
//            //新的信息
//            $statusMsg[$order_info['statusCode']] = $order_info['statusMsg'];


            $update = [
                //取消原因
                'cancel_reason'     => $text['cancel_reason'],
                //取消来源
                'cancel_from'       => $order_info['statusCode']==7?4:$text['cancel_from'],
                //骑手名字
                'transporter_name'  => $text['dm_name'],
                //骑手电话
                'transporter_phone' => $text['dm_mobile'],
                //骑手id
                'transporter_id'    => $text['dm_id'],
                //
                'transporter_lng'   => $order_info['transporterLng'],

                'transporter_lat'   => $order_info['transporterLat'],
                //配送费
                'delivery_fee'      => $order_info['deliveryFee'],
                //距离
                'distance'          => $order_info['distance'],
                //接单时间
                'accept_time'       => !empty($order_info['acceptTime'])?strtotime($order_info['acceptTime']):0,
                //取货时间
                'fetch_time'        => !empty($order_info['fetchTime'])?strtotime($order_info['fetchTime']):0,
                //送达时间
                'finish_time'       => !empty($order_info['finishTime'])?strtotime($order_info['finishTime']):0,
                //取消时间
                'cancel_time'       => !empty($order_info['cancelTime'])?strtotime($order_info['cancelTime']):0,
                //目前状态 等于7其实是超时
                'status'            => $order_info['statusCode']==7?5:$order_info['statusCode'],
                //状态描述
//                'status_msg'        => serialize($statusMsg)
            ];

            if(!empty($text['deduct_fee'])){

                $update['deduct_fee'] = $text['deduct_fee'];

            }

            $transporter_model->where(['id'=>$info['id']])->update($update);

            $msg = $this->progressText($text);

            if(!empty($msg)){
                //录入订单进度
                $pro_inser = [

                    'uniacid'    => $info['uniacid'],

                    'order_code' => $info['order_code'],

                    'msg'        => $msg['msg'],

                    'code'       => $text['order_status'],

                    'time'       => $msg['time']
                ];

                $progress_model = new Progress();

                $progress_model->dataAdd($pro_inser);
            }

        }

        echo true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-02 16:42
     * @功能说明:配送进度话语
     */

    public function progressText($text){


            switch ($text['order_status']){

                case 0:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '商家待接单',

                    ];

                    break;

                case 1:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '商家已接单',

                    ];

                    break;

                case 2:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '骑手已接单'
                    ];

                    break;

                case 3:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '骑手已取货'
                    ];
                    break;
                case 4:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '商品已送达'
                    ];
                    break;

                case 5:

                    if($text['cancel_from']==1){

                        $resaon = '达达配送员取消,取消原因:'.$text['cancel_reason'];

                    }elseif ($text['cancel_from']==2){

                        $resaon = '商家主动取消,取消原因:'.$text['cancel_reason'];

                    }else{

                        $resaon = '系统或客服取消,取消原因:'.$text['cancel_reason'];

                    }

                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => $resaon
                    ];
                    break;

                case 7:
                    $order_pro = [

                        'time' => $text['update_time'],

                        'msg'  => '外卖平台接单超时'
                    ];
                    break;

                case 8:
                    $order_pro = [

                        'time' =>$text['update_time'],

                        'msg'  => '指派单'
                    ];
                    break;

                case 9:
                    $order_pro = [

                        'time' =>$text['update_time'],

                        'msg'  => '妥投异常之物品返回中'
                    ];
                    break;

                case 10:
                    $order_pro = [

                        'time' =>  $text['update_time'],

                        'msg'  => '妥投异常之物品返回完成'
                    ];
                    break;
                case 100:
                    $order_pro = [

                        'time' =>$text['update_time'],

                        'msg'  => '骑士到店'
                    ];
                    break;

                case 1000:
                    $order_pro = [

                        'time' =>$text['update_time'],

                        'msg'  => '创建达达运单失败'
                    ];
                    break;
                default:

                    $order_pro = [];

            }

            return $order_pro;

    }










}
